﻿using Gobang.Client.Common;
using Gobang.ToolClass;
using Gobang.ToolClass.Area;
using Gobang.ToolClass.Model;
using Sunny.UI;
using System.Net;

namespace Gobang.Client.View
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
            NetworkingCenter.LoginAction = LoginMethod;
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            NetworkingCenter.Dispose();
            this.Close();
        }

        private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
        }
        /// <summary>
        /// 缩小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReduce_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            if (string.IsNullOrWhiteSpace(userName))
            {
                UINotifierHelper.ShowNotifier("请填写账号", UINotifierType.WARNING, UILocalize.WarningTitle, false, 2000);
                return;
            }

            if (string.IsNullOrWhiteSpace(pwd))
            {
                UINotifierHelper.ShowNotifier("请填写密码", UINotifierType.WARNING, UILocalize.WarningTitle, false, 2000);
                return;
            }

            UIWaitFormService.ShowWaitForm("系统正在处理中，请稍候...");

            MsgMergeModel msgMergeModel = new MsgMergeModel();
            UserInfoModel userInfoModel = new UserInfoModel
            {
                Accountnumber = userName,
                Password = pwd,
            };
            msgMergeModel.UserInfo = userInfoModel;
            msgMergeModel.SendMsgType = ToolClass.MsgType.Command;
            msgMergeModel.SendMsgCommand = ToolClass.MsgCommand.Logining;
            var bytes = msgMergeModel.GetBytes();


            NetworkingCenter.SendToServer(bytes);


        }
        private void LoginMethod(MsgMergeModel msgMerge, IPEndPoint iPEndPoint)
        {
            Thread.Sleep(300);
            if (msgMerge.IsSuccess)
            {
                this.ExecBeginInvoke(() =>
                {
                    UINotifierHelper.ShowNotifier("登录成功", UINotifierType.OK, UILocalize.SuccessTitle, false, 2000);
                    this.DialogResult = DialogResult.OK;
                    UserInfoCache.UserInfo = msgMerge.UserInfo;
                    AreaCache.AreaNodes = msgMerge.DataStr.DeserializeObject<AreaNode[]>();
                    UIWaitFormService.HideWaitForm();
                    this.Close();
                });
            }
            else
            {
                this.ExecBeginInvoke(() =>
                {
                    UINotifierHelper.ShowNotifier("登录失败，账号或密码错误", UINotifierType.ERROR, UILocalize.WarningTitle, false, 2000);
                    UIWaitFormService.HideWaitForm();
                });
            }
        }
        private void sbtnRegister_Click(object sender, EventArgs e)
        {
            //this.WindowState=FormWindowState.Minimized;
            this.Hide();
            using (FrmRegister fr = new FrmRegister())
            {
                fr.ShowDialog();
            }
            //this.WindowState = FormWindowState.Normal;
            this.Show();
        }
    }
}
